package GEMonitor;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MysqlHelper {
    SqlSession sqlSession = null;

    public boolean connect() {
        // 指定全局配置文件
        final String resource = "mybatis-config.xml";
        final String properties = "jdbc.properties";
        // 读取配置文件
        InputStream inputStream = null;
        InputStream in = null;
        try {
            MyLogger.Info("starting connect to db...");
            in = Resources.getResourceAsStream(properties);
            Properties props = new Properties();
            props.load(in);
            props.put("username",System.getenv("DB_USERNAME"));
            props.put("password",System.getenv("DB_PASSWORD"));
            inputStream = Resources.getResourceAsStream(resource);
            // 构建sqlSessionFactory
            final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,props);
            sqlSession = sqlSessionFactory.openSession();

            MyLogger.Info("db connected success!");

            return true;
        } catch (final IOException e) {
            e.printStackTrace();
            MyLogger.Error("db connect failed: " + e.getMessage());
            return false;
        }
    }

    public GEMsg getCommand() {
        try {
            if (sqlSession == null) {
                if (!connect()) {
                    return null;
                }
            }
            sqlSession.clearCache();
            sqlSession.update("MyMapper.setFinished", -1);
            MSG cmd = sqlSession.selectOne("MyMapper.getCommand");
            sqlSession.commit();
            if (cmd == null) {
                return null;
            }

            sqlSession.update("MyMapper.setFinished", cmd.id);
            sqlSession.commit();

            GEMsg gemsg = new GEMsg();
            gemsg.msg_type = cmd.msg_type;
            gemsg.systemID = cmd.system_id;
            gemsg.content = cmd.msg_content.getBytes();
            return gemsg;

        } catch (final Exception e) {
            e.printStackTrace();
            sqlSession = null;
            MyLogger.Error("ge command form db failed: " + e.getMessage());
            return null;
        }
    }

    public void addReport(final GEMsg msg) {
        try {
            if (sqlSession == null) {
                if (!connect()) {
                    return;
                }
            }

            final MSG m = new MSG();
            m.msg_type = msg.msg_type;
            m.system_id = msg.systemID;
            m.msg_content = new String(msg.content);
            sqlSession.insert("MyMapper.addReport", m);
            sqlSession.commit();

        } catch (final Exception e) {
            e.printStackTrace();
            MyLogger.Error("add report to db failed: " + e.getMessage());
            sqlSession = null;
        }
    }

}